package com.sg.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import com.sg.untils.JDBCUtils;

public class Dao implements IDao {
	Connection conn = null;
	PreparedStatement ps = null;
	ResultSet rs = null;

	@Override
	public int getAZ() {
		try {
			conn = JDBCUtils.getConnection();

			ps = conn.prepareStatement("update z set z=z+1 where id =1");
			ps.executeUpdate();

			ps = conn.prepareStatement("select z from z where id =1");
			rs = ps.executeQuery();
			rs.next();
			int count = rs.getInt("z");
			return count;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public int getZ() {
		try {
			conn = JDBCUtils.getConnection();
			ps = conn.prepareStatement("select z from z where id =1");
			rs = ps.executeQuery();
			rs.next();
			int count = rs.getInt("z");

			return count;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public String isLogin(String zh, String mm) {
		try {
			String count = null;
			String type = null;
			conn = JDBCUtils.getConnection();
			ps = conn.prepareStatement("select count(*) from login where zh=? and mm=?");
			ps.setString(1, zh);
			ps.setString(2, mm);
			rs = ps.executeQuery();
			if (rs.next()) {
				count = rs.getString("count(*)");
			}
			if ("1".equals(count)) {
				ps = conn.prepareStatement("select type from login where zh=? and mm=?");
				ps.setString(1, zh);
				ps.setString(2, mm);
				rs = ps.executeQuery();
				if (rs.next()) {
					type = rs.getString("type");
				}
				if("0".equals(type)) {
					return "true0";				
				}else {		
					return "true1";
				}
			}

			return "false";
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	/*
	 * @Override public Map<String, Object> getPL() { try { Map<String, Object> map
	 * = new HashMap<String, Object>(); int x = 0; conn = JDBCUtils.getConnection();
	 * ps = conn.prepareStatement("select pl from pl"); rs = ps.executeQuery();
	 * while (rs.next()) { map.put(x + "", rs.getString("pl")); x++; } return map; }
	 * catch (SQLException e) { e.printStackTrace(); } return null; }
	 */
	@Override
	public Map<String, Object> getPL() {
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			int x = 0;
			conn = JDBCUtils.getConnection();
			ps = conn.prepareStatement("select id,pl from pl");
			rs = ps.executeQuery();
			while (rs.next()) {
				map.put(x + "",  rs.getString("id")+":"+rs.getString("pl"));
				x++;
			}
			return map;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public Map<String, Object> getAPL(String s) {
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			int x = 0;
			conn = JDBCUtils.getConnection();
			ps = conn.prepareStatement("INSERT INTO pl(pl) VALUES(?)");
			ps.setString(1, s);
			ps.executeUpdate();
			
			ps = conn.prepareStatement("select pl from pl");
			rs = ps.executeQuery();
			while (rs.next()) {
				map.put(x + "", rs.getString("pl"));
				x++;
			}
			return map;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

}
